package com.bdqn.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.base.R;
import com.bdqn.base.logger.WebLogger;
import com.bdqn.pojo.ShopAddress;
import com.bdqn.service.ShopAddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 收获地址
 *
 * @author lilibo
 * @date 2021/10/09
 */
@Api(tags="收获地址接口")
@RestController
@RequestMapping("/shopaddress")
public class ShopAddressController {

    @Autowired
    private ShopAddressService shopAddressService;

    /**
     * 新增收获地址记录
     *
     * @param shopAddress
     * @return R
     */
    @ApiOperation("新增收获地址记录")
    @WebLogger("新增收获地址记录")
    @PostMapping("/add")
    public R save(@ApiParam("收获地址对象") @RequestBody ShopAddress shopAddress) {
        return new R<>(shopAddressService.save(shopAddress));
    }

    /**
     * 修改收获地址记录
     *
     * @param shopAddress
     * @return R
     */
    @ApiOperation("修改收获地址记录")
    @WebLogger("修改收获地址记录")
    @PostMapping("/update")
    public R update(@ApiParam("收获地址对象") @RequestBody ShopAddress shopAddress) {
        return new R<>(shopAddressService.updateById(shopAddress));
    }

    /**
     * 通过id删除一条收获地址记录
     *
     * @param id
     * @return R
     */
    @ApiOperation("通过id删除一条收获地址记录")
    @WebLogger("通过id删除一条收获地址记录")
    @PostMapping("/delete")
    public R removeById(@ApiParam("收获地址id") Integer id) {
        return new R<>(shopAddressService.removeById(id));
    }

    /**
     * 通过id查询单条收获地址记录
     *
     * @param id
     * @return R
     */
    @ApiOperation("通过id查询单条收获地址记录")
    @WebLogger("通过id查询单条收获地址记录")
    @GetMapping("/{id}")
    public R<ShopAddress> getById(@ApiParam("收获地址id") @PathVariable Integer id) {
        return new R<>(shopAddressService.getById(id));
    }

    /**
     * 通过收获地址信息查询收获地址记录
     *
     * @param shopAddress 收获地址信息对象
     * @return R
     */
    @ApiOperation("通过收获地址信息查询收获地址记录")
    @WebLogger("通过收获地址信息查询收获地址记录")
    @GetMapping("/list")
    public R<List<ShopAddress>> getList(@ApiParam("查询的收获地址对象") ShopAddress shopAddress) {
        return new R<>(shopAddressService.getList(shopAddress));
    }

    /**
     * 分页查询收获地址
     *
     * @param pageNo 当前页
     * @param pageSize 每页条数
     * @param shopAddress 收获地址
     * @return
     */
    @ApiOperation("分页查询收获地址")
    @WebLogger("分页查询收获地址")
    @GetMapping("/page")
    public R<IPage<ShopAddress>> getPage(@ApiParam("当前页") @RequestParam(required = false, defaultValue = "1")  Long pageNo, @ApiParam("每页条数") @RequestParam(required = false, defaultValue = "10") Long pageSize, @ApiParam("查询的收获地址对象") ShopAddress shopAddress) {
        return new R<>(shopAddressService.getPage(new Page<>(pageNo, pageSize), shopAddress));
    }

    /**
     * 分页查询收获地址数据
     *
     * @param pageNo 当前页
     * @param pageSize 每页条数
     * @param shopAddress 收获地址
     * @return 返回对象中的count为数据总量
     */
    @ApiOperation("分页查询收获地址数据")
    @WebLogger("分页查询收获地址数据")
    @GetMapping("/pagelist")
    public R<List<ShopAddress>> getPageList(@ApiParam("当前页") @RequestParam(required = false, defaultValue = "1")  Long pageNo, @ApiParam("每页条数") @RequestParam(required = false, defaultValue = "10") Long pageSize, @ApiParam("查询的收获地址对象") ShopAddress shopAddress) {
        IPage<ShopAddress> page = shopAddressService.getPage(new Page<>(pageNo, pageSize), shopAddress);
        return new R<>(page.getRecords(), page.getTotal());
    }

}
